from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response

from goods.models import SKU
from utils.pagination import StandardResultsSetPagination

"""
所谓的静态化其实就是让用户直接去访问我们处理好的html页面

问题：
	我们首页的数据会发生变化

1.先查询数据库的数据
2.通过模板来渲染数据
3.模板渲染好的html 写入到指定的文件目录

"""

"""
用户选择不同的分类，我们返回相应的热销商品，返回2个

1.接收分类id
2.根据分类id 获取商品信息，【sku,sku】
3.进行序列化操作（将对象转换为字典，json）
4.返回相应

GET			/goods/categories/(?P<category_id>\d+)/hotskus
"""
from rest_framework.views import APIView
from .serializers import HotSKUSerializer
# class HotSKUView(APIView):
#
# 	def get(self,request,category_id):
#
# 		# 1.接收分类id
# 		# 2.根据分类id 获取商品信息，【sku,sku】
#
# 		# 分类
# 		# category 相当于一个实例对象 category = 对象
# 		# 如果你想给category赋值一个id
# 		# 这时候你只能使用 数据表中的字段名 = 值
# 		# category_id = 1
# 		# category = GoodCategory.objects.get(category_id = category_id)
#
# 		# 上架
# 		# where category_id = xxx and is_lanuched = True
#
# 		skus = SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]
#
# 		# 3.进行序列化操作（将对象转换为字典，json）
# 		serializer = HotSKUSerializer(skus,many=True)
# 		# 4.返回相应
# 		return Response(serializer.data)

from rest_framework.generics import ListAPIView
class HotSKUView(ListAPIView):

	pagination_class = None

	serializer_class = HotSKUSerializer

	# # queryset = SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]
	def get_queryset(self):
		category_id = self.kwargs['category_id']
		return SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]

from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination
class SKUAPIView(ListAPIView):
	"""

	1.根据分类获取所有数据
	2.排序
	3.分页

	GET		/goods/categories/(?P<category_id>\d+)/skus/?ordering=-price&page=2&page_size=2

	"""

	# 排序实现
	filter_backends = [OrderingFilter]
	ordering_fields = ['price','sales','create_time']

	# 分页
	# pagination_class = StandardResultsSetPagination

	serializer_class = HotSKUSerializer

	# queryset = SKU.objects.filter(is_launched=True,category_id=category_id)

	def get_queryset(self):
		category_id = self.kwargs['category_id']
		return SKU.objects.filter(is_launched=True,category_id=category_id)

	"""
	搜索的步骤分为 2步：
	一   elasticsearch 帮助我们实现搜索引擎的功能
			我们通过docker的方式让它运行起来
	二	haystack 区链接elasticsearch
		配置 haystack
		定义搜索索引类，让elasticsearch 取分词来创建 全文索引
		索引

	"""

from .serializers import SKUIndexSerializer
from drf_haystack.viewsets import HaystackViewSet

class SKUSearchViewSet(HaystackViewSet):
	"""
    SKU搜索
    """
	index_models = [SKU]

	serializer_class = SKUIndexSerializer

